package com.onstar.cn.weight;

import com.onstar.cn.common.util.DeviceUtil;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * @author jun chen
 * @ClassName: HeadViewLayout 
 * @date 2016年1月28日 上午10:03:25 
 * @Description: 
 */
public class HeadViewLayout extends RelativeLayout
{
	private View view;
	private RelativeLayout headview_layout;
	private ImageButton image_btn_back;
	private ImageButton image_btn_right;
	private TextView headview_title;

	public HeadViewLayout(Context context) {
		super(context);
		
		initView(null);
	}

	public HeadViewLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(attrs);
	}

	
	private void initView(AttributeSet attrs){
		view = inflate(getContext(), R.layout.onstar_nav_headview, this);
		headview_layout = (RelativeLayout) view.findViewById(R.id.headview_layout);
		image_btn_back = (ImageButton) view.findViewById(R.id.image_btn_back);
		image_btn_right = (ImageButton) view.findViewById(R.id.image_btn_right);
		headview_title = (TextView) view.findViewById(R.id.headview_title);
		
		image_btn_back.setImageResource(R.drawable.btn_back_selectot);
		if(DeviceUtil.getLanguage().equalsIgnoreCase("en")){
			headview_title.setTextSize(15);
		}else{
			headview_title.setTextSize(22);
		}
		
		if(attrs !=null){
			TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.headviewstyle);
			
			String title = typedArray.getString(R.styleable.headviewstyle_headviewTitle);
			boolean isShowRbtn = typedArray.getBoolean(R.styleable.headviewstyle_isShowRbtn, true);
			Drawable rbtnBg = typedArray.getDrawable(R.styleable.headviewstyle_rbtnBackground);
			int rbtnres = typedArray.getResourceId(R.styleable.headviewstyle_rbtnBackground, 0);
			int headviewbgcolor = typedArray.getColor(R.styleable.headviewstyle_headviewbg, Color.BLACK);
			
			headview_layout.setBackgroundColor(headviewbgcolor);
			if(!TextUtils.isEmpty(title)){
				headview_title.setText(title);
			}
			
			if(isShowRbtn){
				image_btn_right.setVisibility(CustomImageView.VISIBLE);
				//image_btn_right.setBackground(rbtnBg);
				//image_btn_right.setBackgroundResource(rbtnres);
				image_btn_right.setImageResource(rbtnres);
			}else{
				image_btn_right.setVisibility(CustomImageView.GONE);
			}
			
			typedArray.recycle();
		}
		
		image_btn_back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (getContext() instanceof Activity) {
					Activity activity = (Activity)getContext();
					activity.setResult(Activity.RESULT_CANCELED);
					((Activity) getContext()).finish();
				}
			}
		});
		
		image_btn_right.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
			}
		});
		
		
	}

	
	public void setLeftBtnOnClickListener(OnClickListener onClickListener){
		image_btn_back.setOnClickListener(onClickListener);
	}
	
	public void setRbtnOnClickListener(OnClickListener onClickListener){
		image_btn_right.setOnClickListener(onClickListener);
	}
	
	public void setRbtnOnClickListener(int drawableResId, OnClickListener onClickListener){
		image_btn_right.setImageResource(drawableResId);
		image_btn_right.setOnClickListener(onClickListener);
		image_btn_right.setVisibility(View.VISIBLE);
	}
	
	
	public void setHeadviewTitle(String title){
		if (title != null && title.length() > 20) {
			title = title.subSequence(0, 18) + "..";
		}
		headview_title.setText(title);
	}
	
	public void setHeadviewTitle(int titleId){
		setHeadviewTitle(getResources().getString(titleId));
	}
	
	public ImageButton getImageButton(){
		return image_btn_right;
	}
	
	
	
	
	
	
}
